home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / os2tools / aping / cpicport.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-15  |  11.2 KB  |  331 lines

  1. /*****************************************************************************
  2.  *
  3.  *  MODULE NAME: CPICPORT.C
  4.  *
  5.  *  COPYRIGHTS:
  6.  *             This module contains code made available by IBM
  7.  *             Corporation on an AS IS basis.  Any one receiving the
  8.  *             module is considered to be licensed under IBM copyrights
  9.  *             to use the IBM-provided source code in any way he or she
  10.  *             deems fit, including copying it, compiling it, modifying
  11.  *             it, and redistributing it, with or without
  12.  *             modifications.  No license under any IBM patents or
  13.  *             patent applications is to be implied from this copyright
  14.  *             license.
  15.  *
  16.  *             A user of the module should understand that IBM cannot
  17.  *             provide technical support for the module and will not be
  18.  *             responsible for any consequences of use of the program.
  19.  *
  20.  *             Any notices, including this one, are not to be removed
  21.  *             from the module without the prior written consent of
  22.  *             IBM.
  23.  *
  24.  *  AUTHOR:    Peter J. Schwaller
  25.  *             VNET:     PJS at RALVM6           Tie Line: 444-4376
  26.  *             Internet: pjs@ralvm6.vnet.ibm.com     (919) 254-4376
  27.  *
  28.  *  FUNCTION:  Contains procedures to that may have to be rewritten for
  29.  *             different environments.
  30.  *
  31.  *  AVAILABILITY:
  32.  *             These sample programs and source are also available on
  33.  *             CompuServe through the APPC Information Exchange.  To get
  34.  *             to the APPC forum just type 'GO APPC' from any CompuServe
  35.  *             prompt.  The samples are available in the Sample Programs
  36.  *             library section.  Just search on the keyword CPICPGMS to
  37.  *             find all the samples in this series.
  38.  *
  39.  *             Updates for the sample programs and support for many more
  40.  *             CPI-C platforms will also be made available on CompuServe.
  41.  *
  42.  *  RELATED FILES:
  43.  *             CPICPORT.H
  44.  *
  45.  *****************************************************************************/
  46.  
  47.  
  48. /* if on OS/2, get the OS/2 include file
  49.  * needed for:
  50.  *     DosAllocSeg
  51.  *     DosGetMachineMode
  52.  */
  53. #if defined(OS2) || defined(FAPI) || defined(OS2_20)
  54. #define  INCL_BASE
  55. #include <os2.h>
  56. #endif
  57. #include "cpicport.h"
  58.  
  59.  
  60. /* standard C include files */
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63. #include <string.h>
  64.  
  65. /*
  66.  * Some platforms do not have the strupr C library routine, so we will
  67.  * define it here.  strupr converts a string to uppercase.
  68.  */
  69.  
  70. #if defined(OS400) || defined(VM) || defined(MVS) || defined(AIX)
  71. int
  72. strupr (char * string)
  73. {
  74.   for (;*string;++string)
  75.   *string=toupper(*string);
  76.   return;
  77. }
  78. #endif
  79.  
  80. /*
  81.  * OS/2 can optimize performance if a special shared memory buffer is
  82.  * used as the data buffer on calls to CMSEND and CMRCV.  To hide this
  83.  * special case from the calling program, this procedure should be
  84.  * called to allocate all CPI-C data buffers.
  85.  */
  86.  
  87. char far *
  88. alloc_cpic_buffer (USHORT size)
  89. {
  90. #if defined(OS2) || defined(FAPI)
  91. #ifndef OS2_20
  92.    USHORT selector;                         /* selector from DosAllocSeg     */
  93.    USHORT dos_rc;                           /* OS/2 return code              */
  94.    char far *memory_pointer;                /* return pointer to memory      */
  95.  
  96.    dos_rc = DosAllocSeg ((unsigned)size,    /* size of memory to allocate    */
  97.                          (PSEL)&selector,   /* returned: selector address    */
  98.                          (unsigned)1);      /* shared, unmamed segment       */
  99.    if (dos_rc == 0) {                       /* Non-zero OS/2 return code?    */
  100.        SELECTOROF(memory_pointer) = selector; /* address = Selector:0        */
  101.        OFFSETOF(memory_pointer) = 0;        /* set the offset to 0           */
  102.        return(memory_pointer);
  103.    } else {
  104.        return malloc(size);
  105.    }
  106. #else
  107.    PVOID address;
  108.    ULONG dos_rc;
  109.  
  110.    dos_rc = DosAllocSharedMem(&address,
  111.                               NULL,
  112.                               size,
  113.                               fALLOCSHR);
  114.  
  115.    if (dos_rc == 0) {                       /* Non-zero OS/2 return code?    */
  116.        return(address);
  117.    } else {
  118.        return malloc(size);
  119.    }
  120. #endif
  121. #else
  122.    return malloc(size);
  123. #endif
  124. }
  125.  
  126.  
  127. void
  128. show_info(char * * text)
  129. /*
  130.  * This procedure displays a block of text information on the the screen.
  131.  * The input argument is an array of strings to be output, one string
  132.  * per line.  A NULL array element indicates the end of the strings.
  133.  */
  134. {
  135.     int i;
  136.  
  137.     for ( i = 0; text[i] != NULL; i++ ) {
  138.         printf("%s\n", text[i]);
  139.     }
  140.     return;
  141. }
  142.  
  143. USHORT
  144. get_machine_mode(void)
  145. /*
  146.  * In OS/2 and DOS, this procedure indicates whether the machine is
  147.  * currently running in real (DOS) or protect (OS/2) mode.
  148.  *   1 - Protect (OS/2)
  149.  *   0 - Real    (DOS)
  150.  */
  151. {
  152. #if defined(OS2) || defined(FAPI)
  153. #ifndef OS2_20
  154.     unsigned char mode;
  155.     USHORT dos_rc;
  156.     USHORT rc;
  157.     dos_rc = DosGetMachineMode(&mode);
  158.     if (!dos_rc) {
  159.         rc = (USHORT)mode;
  160.     } else {
  161.         rc = 0;
  162.     }
  163.     return rc;
  164. #else
  165.     return 1;
  166. #endif
  167. #else
  168.     return 0;
  169. #endif
  170. }
  171.  
  172. int
  173. get_passwd(char * passwd, int max_length)
  174. /*
  175.  * Gets a password from the user.  Where possible, this routine should
  176.  * disable echoing of keystrokes for security reasons.
  177.  *
  178.  * Returns
  179.  * 0 - password was successfully input
  180.  * 1 - password variable was not updated successfully
  181.  */
  182. {
  183.     int rc;
  184. #if defined(OS2) || defined(FAPI)
  185.     STRINGINBUF stringinbuf;
  186.  
  187.     set_echo(FALSE);                        /* keystrokes will not appear    */
  188.  
  189.     stringinbuf.cb = max_length;            /* set the max input size */
  190.  
  191.     /* this call will not allow the user to enter more than the specified    */
  192.     /* maximum number of characters.                                         */
  193.     KbdStringIn((unsigned char *)passwd, &stringinbuf, 0, 0);
  194.  
  195.     passwd[stringinbuf.cchIn] = '\0';       /* ensure NULL termination   */
  196.  
  197.     set_echo(TRUE);                         /* turn echo of keystrokes on    */
  198.     rc = 0;
  199.  
  200. #else
  201.     int length;
  202.  
  203.     /* There is no portable way to disable echoing of input keystrokes.      */
  204.     /* If a platform does support turning off echo, this section should be   */
  205.     /* rewritten and ifdef'ed.                                               */
  206.  
  207.     length = (int) fgets(passwd, max_length+1, stdin);
  208.     if (length > 0 && length < max_length) {
  209.         if (passwd[length-1] == '\n') {     /* remove the trailing          */
  210.             passwd[length-1] = '\0';        /* newline if it exists         */
  211.         }
  212.         rc = 0;
  213.     } else {
  214.         rc = 1;
  215.     }
  216.  
  217. #endif
  218.     return rc;
  219. }
  220.  
  221. void
  222. set_echo(int mode)
  223. {
  224. #if defined(OS2) || defined(FAPI)
  225.    KBDINFO     kbdinfo;                      /* keyboard status info        */
  226.  
  227.    kbdinfo.cb = 10;
  228.    KbdGetStatus(&kbdinfo, 0);
  229.  
  230.    if (mode) {
  231.       /* Keystrokes will be displayed on the screen                         */
  232.       kbdinfo.fsMask |= 1;                   /* set echo on                 */
  233.       kbdinfo.fsMask &= 0xFD;                /* turn on echo                */
  234.    } else {
  235.       /* Keystrokes will not be displayed on the screen                     */
  236.       kbdinfo.fsMask |= 2;                   /* set echo off                */
  237.       kbdinfo.fsMask &= 0xFE;                /* turn off echo               */
  238.    }
  239.  
  240.    KbdSetStatus(&kbdinfo, 0);
  241. #else
  242.  
  243. #endif
  244.  
  245.    return;
  246. }
  247.  
  248.  
  249. void   ascii_to_ebcdic_field  (char * ascii_field,
  250.                                USHORT field_size)
  251. {
  252.     USHORT i;
  253.  
  254.     for (i = 0;
  255.          i < field_size;
  256.          ascii_field[i] = ascii_to_ebcdic_table[(unsigned)ascii_field[i]],i++);
  257. }
  258.  
  259. void  ascii_to_ebcdic_string (char * ascii_string)
  260. {
  261.     ascii_to_ebcdic_field(ascii_string, strlen(ascii_string));
  262. }
  263.  
  264. void   ebcdic_to_ascii_field  (char * ebcdic_field,
  265.                                USHORT field_size)
  266. {
  267.     USHORT i;
  268.  
  269.     for (i = 0;
  270.          i < field_size;
  271.          ebcdic_field[i] =
  272.                          ebcdic_to_ascii_table[(unsigned)ebcdic_field[i]],i++);
  273. }
  274.  
  275. void  ebcdic_to_ascii_string (char * ebcdic_string)
  276. {
  277.     ebcdic_to_ascii_field(ebcdic_string, strlen(ebcdic_string));
  278. }
  279.  
  280.  
  281.  
  282. /* ASCII to EBCDIC translate table (only UGL character set) */
  283.  
  284. char ascii_to_ebcdic_table[] = {
  285. #if 0
  286. /* PJS */
  287. "\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x25\x0B\x0C\x0D\x0E\x0F"  /* 00-0F */
  288. #endif
  289. "\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x15\x0B\x0C\x0D\x0E\x0F"  /* 00-0F */
  290. "\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x22\x1D\x35\x1F"  /* 10-1F */
  291. "\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61"  /* 20-2F */
  292. "\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F"  /* 30-3F */
  293. "\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6"  /* 40-4F */
  294. "\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xAD\xE0\xBD\x5F\x6D"  /* 50-5F */
  295. "\x79\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96"  /* 60-6F */
  296. "\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xC0\x4F\xD0\xA1\x07"  /* 70-7F */
  297. "\x43\x20\x21\x1C\x23\xEB\x24\x9B\x71\x28\x38\x49\x90\xBA\xEC\xDF"  /* 80-8F */
  298. "\x45\x29\x2A\x9D\x72\x2B\x8A\x9A\x67\x56\x64\x4A\x53\x68\x59\x46"  /* 90-9F */
  299. "\xEA\xDA\x2C\xDE\x8B\x55\x41\xFE\x58\x51\x52\x48\x69\xDB\x8E\x8D"  /* A0-AF */
  300. "\x73\x74\x75\xFA\x15\xB0\xB1\xB3\xB4\xB5\x6A\xB7\xB8\xB9\xCC\xBC"  /* B0-BF */
  301. "\xAB\x3E\x3B\x0A\xBF\x8F\x3A\x14\xA0\x17\xCB\xCA\x1A\x1B\x9C\x04"  /* C0-CF */
  302. "\x34\xEF\x1E\x06\x08\x09\x77\x70\xBE\xBB\xAC\x54\x63\x65\x66\x62"  /* D0-DF */
  303. "\x30\x42\x47\x57\xEE\x33\xB6\xE1\xCD\xED\x36\x44\xCE\xCF\x31\xAA"  /* E0-EF */
  304. "\xFC\x9E\xAE\x8C\xDD\xDC\x39\xFB\x80\xAF\xFD\x78\x76\xB2\x9F\xFF"  /* F0-FF */
  305. };
  306.  
  307. /* EBCDIC to ASCII translate table (only UGL character set) */
  308.  
  309. char ebcdic_to_ascii_table[] = {
  310. "\x00\x01\x02\x03\xCF\x09\xD3\x7F\xD4\xD5\xC3\x0B\x0C\x0D\x0E\x0F"  /* 00-0F */
  311. #if 0
  312. PJS fix for strange value for \n on VM
  313. "\x10\x11\x12\x13\xC7\xB4\x08\xC9\x18\x19\xCC\xCD\x83\x1D\xD2\x1F"  /* 10-1F */
  314. #endif
  315. "\x10\x11\x12\x13\xC7\x0A\x08\xC9\x18\x19\xCC\xCD\x83\x1D\xD2\x1F"  /* 10-1F */
  316. "\x81\x82\x1C\x84\x86\x0A\x17\x1B\x89\x91\x92\x95\xA2\x05\x06\x07"  /* 20-2F */
  317. "\xE0\xEE\x16\xE5\xD0\x1E\xEA\x04\x8A\xF6\xC6\xC2\x14\x15\xC1\x1A"  /* 30-3F */
  318. "\x20\xA6\xE1\x80\xEB\x90\x9F\xE2\xAB\x8B\x9B\x2E\x3C\x28\x2B\x7C"  /* 40-4F */
  319. "\x26\xA9\xAA\x9C\xDB\xA5\x99\xE3\xA8\x9E\x21\x24\x2A\x29\x3B\x5E"  /* 50-5F */
  320. "\x2D\x2F\xDF\xDC\x9A\xDD\xDE\x98\x9D\xAC\xBA\x2C\x25\x5F\x3E\x3F"  /* 60-6F */
  321. "\xD7\x88\x94\xB0\xB1\xB2\xFC\xD6\xFB\x60\x3A\x23\x40\x27\x3D\x22"  /* 70-7F */
  322. "\xF8\x61\x62\x63\x64\x65\x66\x67\x68\x69\x96\xA4\xF3\xAF\xAE\xC5"  /* 80-8F */
  323. "\x8C\x6A\x6B\x6C\x6D\x6E\x6F\x70\x71\x72\x97\x87\xCE\x93\xF1\xFE"  /* 90-9F */
  324. "\xC8\x7E\x73\x74\x75\x76\x77\x78\x79\x7A\xEF\xC0\xDA\x5B\xF2\xF9"  /* A0-AF */
  325. "\xB5\xB6\xFD\xB7\xB8\xB9\xE6\xBB\xBC\xBD\x8D\xD9\xBF\x5D\xD8\xC4"  /* B0-BF */
  326. "\x7B\x41\x42\x43\x44\x45\x46\x47\x48\x49\xCB\xCA\xBE\xE8\xEC\xED"  /* C0-CF */
  327. "\x7D\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\xA1\xAD\xF5\xF4\xA3\x8F"  /* D0-DF */
  328. "\x5C\xE7\x53\x54\x55\x56\x57\x58\x59\x5A\xA0\x85\x8E\xE9\xE4\xD1"  /* E0-EF */
  329. "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\xB3\xF7\xF0\xFA\xA7\xFF"  /* F0-FF */
  330. };
  331.